package com.songcha.module_category.ui.fragment.category_child

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.ViewGroup.FOCUS_BLOCK_DESCENDANTS
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.songcha.library_base.mvvm.base.BaseRefreshLoadMoreMvvmFragment
import com.songcha.library_business.bean.advert.AdvertInfoBean
import com.songcha.library_business.bean.book.BookNetBean
import com.songcha.library_business.config.VarConfig
import com.songcha.library_business.handler.BookUriHandler
import com.songcha.library_business.helper.AdvertCoverHelper
import com.songcha.library_business.helper.AdvertHelper
import com.songcha.library_business.interfaces.IOnBookClickListener
import com.songcha.library_business.ui.adapter.BookListAdapter
import com.songcha.library_business.ui.adapter.SimpleBannerAdapter
import com.songcha.library_business.util.BusinessJumpUtil
import com.songcha.library_business.util.MajiaBaoUtil
import com.songcha.library_common.helper.ViewHelper
import com.songcha.library_common.util.ColorUtil
import com.songcha.module_category.R
import com.songcha.module_category.bean.CategoryChannelSubListBean
import com.songcha.module_category.databinding.CategoryFragmentCategorySubBinding
import com.songcha.module_category.structs.CategorySubMultiItem
import com.songcha.module_category.ui.activity.category_detail.CategoryDetailActivity
import com.songcha.module_category.ui.adapter.CategorySubAdapter
import com.songcha.module_category.ui.decoraction.CategorySubItemDecoration
import com.zhpan.bannerview.BaseBannerAdapter
import com.zhpan.bannerview.constants.PageStyle

class CategoryChildFragment:BaseRefreshLoadMoreMvvmFragment<CategoryChildViewModel,CategoryFragmentCategorySubBinding>(),com.chad.library.adapter.base.listener.OnItemClickListener,IOnBookClickListener {
    private var mTopAdapter:CategorySubAdapter? = null
    private var mEndBookAdapter:BookListAdapter? = null

    override fun getLayoutId(): Int {
        return R.layout.category_fragment_category_sub
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        arguments?.let {
            viewModel.channelId = it.getInt("channel_id")
            viewModel.position = it.getInt("position")
        }

        if(viewModel.channelId <= 0) return

        //initRcv()
       /* viewModel.categorySubList.observe(this){data->
            initTopRcv(data)
        }

        viewModel.categorySubEndBookList.observe(this){data->
            initEndBookRcv(data)
        }*/
        //initAdvert()
        if(viewModel.position == 0 && MajiaBaoUtil.getMajiaName() == "beike"){
            ViewHelper.setVisibility(true,databinding.cateFlBanner)
        }else{
            ViewHelper.setVisibility(false,databinding.cateFlBanner)
        }
    }

    override fun initObserver() {
        viewModel.bannerList.observe(this){data->
            if(data == null) return@observe

            ViewHelper.setVisibility(data.isNotEmpty(),databinding.cateFlBanner)

            databinding.cateItemBvp.apply {
                adapter = SimpleBannerAdapter() as BaseBannerAdapter<Any>
                setPageStyle(PageStyle.NORMAL)
                //setRevealWidth(20.dp2px())
                setIndicatorSliderColor(
                    ColorUtil.getResourceColor(context, com.songcha.library_common.R.color.banner_indictor_normal),
                    ColorUtil.getResourceColor(context, com.songcha.library_common.R.color.banner_indictor_selected))
                registerLifecycleObserver(lifecycle)

                setOnPageClickListener{v,p->
                    BookUriHandler.handle(data[p].tarUrl)
                }
            }.create(data)
            //small screen device may cannot display
            //bvp.refreshData(bannerList)
            databinding.cateItemBvp.descendantFocusability= FOCUS_BLOCK_DESCENDANTS
        }
    }

    override fun onResume() {
        super.onResume()
        initAdvert()
    }

    private fun initAdvert(){
        if(viewModel.position > 0) return
        AdvertCoverHelper.loadAdvertByCover(requireActivity(),AdvertHelper.ADVERT_TYPE_BANNER, VarConfig.ADVERT_CATEGORY_BANNER_INFO,
            databinding.cateFlAdvert,80f,object :AdvertHelper.ILoadAdvertCommonListener{
                override fun onSuccess(type: Int, height: Int, view: View?) {
                    ViewHelper.setVisibility(true,databinding.cateFlAdvert)
                    if(type == AdvertInfoBean.ADVERT_TYPE_CSJ){
                        //ViewHelper.setVisibility(true,cclAdvert)
                        databinding.cateFlAdvert.removeAllViews()
                        databinding.cateFlAdvert.addView(view)
                    }else{

                    }
                }

                override fun onError(type: Int,code:Int,msg:String) {
                    ViewHelper.setVisibility(false,databinding.cateFlAdvert)
                    //ViewHelper.setVisibility(false,cclAdvert)
                    databinding.cateFlAdvert.removeAllViews()
                }

                override fun onClose(type: Int) {
                    ViewHelper.setVisibility(false,databinding.cateFlAdvert)
                    //ViewHelper.setVisibility(false,cclAdvert)
                    databinding.cateFlAdvert.removeAllViews()
                }

            })
    }

    override fun onGetData() {
        //viewModel.getCategoryChannelList()
        //viewModel.getCategorySubEndBookList()
        if(viewModel.channelId <= 0) return
        viewModel.getData{
            if(viewModel.position == 0 && MajiaBaoUtil.getMajiaName() == "beike"){
                viewModel.getBanner()
            }
        }
    }

    override fun onGetLoadMoreData() {
        viewModel.getCategorySubEndBookList()
    }

    fun initTopRcv(list:MutableList<CategoryChannelSubListBean.ChannelSubBean>){
        databinding.categorySubRcvTop.apply {
            layoutManager = GridLayoutManager(context,2)
            mTopAdapter = CategorySubAdapter(list)
            mTopAdapter!!.setOnItemClickListener(this@CategoryChildFragment)
            adapter = mTopAdapter
            if(itemDecorationCount == 0)
                addItemDecoration(CategorySubItemDecoration())
        }
    }

    fun initEndBookRcv(list:MutableList<BookNetBean>){
        databinding.categorySubRcvBottom.apply {
            layoutManager = LinearLayoutManager(context)
            mEndBookAdapter = BookListAdapter(list,this@CategoryChildFragment,false,true)
            adapter = mEndBookAdapter
        }
    }

    override fun initListData() {
        if(viewModel.rlst.value == null) return
        if(viewModel.rlst.value!!.size < 2) return
        if(viewModel.rlst.value!![0].type == CategorySubMultiItem.CATEGORY_SUB_MULTI_ITEM_TYPE_TOP){
            initTopRcv(viewModel.rlst.value!![0].data as MutableList<CategoryChannelSubListBean.ChannelSubBean>)
        }
        /*if(viewModel.rlst.value!![1].type == CategorySubMultiItem.CATEGORY_SUB_MULTI_ITEM_TYPE_END_BOOK){
            val list = mutableListOf<BookNetBean>()
            for(i in 1 until viewModel.rlst.value!!.size){
                val item = viewModel.rlst.value!![i].data
                if(item!=null)
                    list.add(item as BookNetBean)
            }
            initEndBookRcv(list)
            //initEndBookRcv(viewModel.rlst.value!![1].data as MutableList<BookNetBean>)
        }*/
    }

    override fun initLoadMoreListData() {
        if(viewModel.rlmlst.value == null) return
        initEndBookRcv(viewModel.rlmlst.value!! as MutableList<BookNetBean>)
    }

    override fun isNeedShowLoad(): Boolean {
        return true
    }

    override fun getRefreshLayout(): SmartRefreshLayout {
       return databinding.categorySubSrl
    }

    override fun getListAdapter(): RecyclerView.Adapter<BaseViewHolder>? {
        return mEndBookAdapter
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        if(adapter !is CategorySubAdapter) return
        if(viewModel.rlst.value == null) return
        if(viewModel.rlst.value!!.size == 0) return
        val intent= Intent(requireContext(), CategoryDetailActivity::class.java)
        intent.putExtra("title",adapter.data[position].catName)
        intent.putExtra("channel_sub_id",adapter.data[position].id)
        requireContext().startActivity(intent)
    }

    override fun onBookClick(bookId: String, img: ImageView) {
        BusinessJumpUtil.toBookDetail(bookId)
    }

}